<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function getDirContent($path)
{
    if (!is_dir($path)) {
        return false;
    }
    $arr = array();
    $data = scandir($path);
    foreach ($data as $value) {
        if ($value != '.' && $value != '..') {
            $arr[] = $value;
        }
    }
    return $arr;
}

function arrayCompare($base, $data)
{
    foreach ($data as $key => $item) {
        if (array_key_exists('ref', $item)) {
            $value = $item['ref'];
            if (in_array($value, array_keys($base))) {
                unset($data[$key]['ref']);
                foreach ($base[$key] as $k => $v) {
                    $data[$key][$k] = $v;
                }
            }
        } else {
            foreach ($item as $k => $c) {
                if (is_array($c) && is_assoc($c)) {
                    $data[$key][$k] = arrayCompare($base, $c);
                }
            }
        }
    }
    return $data;
}

function is_assoc($array)
{
    return array_keys($array) !== range(0, count($array) - 1);
}

function jsonToXml($source, $charset = 'utf8')
{
    if (empty($source)) {
        return false;
    }
    //php5，以及以上，如果是更早版本，请查看JSON.php
    $array = json_decode($source);
    $xml = '';
    $xml .= change($array);
    return $xml;
}

function change($source)
{
    $string = "";
    foreach ($source as $k => $v) {
        $string .= "<" . $k . ">";
        if (is_array($v) || is_object($v)) {
            $string .= change($v);
        } else {
            $string .= $v;
        }
        $string .= "</" . $k . ">";
    }
    return $string;
}


function detailDom($element, $include = null)
{
    ini_set('max_execution_time', '120');
    ini_set('memory_limit', '2048M');
    //获取所有文件结构后进行判断
    //1 首先获得第一层所有节点（顶级不可能存在ref引用）
    $topName = $element['@attributes']['name'];
    if (isset($element['complexType']['sequence'])) {
        $firstElement = $element['complexType']['sequence']['element'];
    } elseif (isset($element['complexType']['choice'])) {
        $firstElement = $element['complexType']['choice']['element'];
    } else {
        $firstElement = $element['complexType']['simpleContent']['extension'];
    }
    //根据不同文件数据结构 判断数据结构(可能是1维数组或者或者二维数组)
    if (!isset($firstElement[0])) {

        //是否有ref引用
        if (isset($firstElement['@attributes']['ref'])) {
            //遍历所有xsd文件查找对应ref
            $Xsd = new \app\index\controller\Xsd();
            foreach ($include as $value) {
                $base = $Xsd->xsdArr('./xsd/' . $value);
                $onlyOne = $base['element'];
                foreach ($onlyOne as $k => $data) {
                    if (isset($data['@attributes']))//二维数组情况
                    {
                        //在该文件有没有和主文件ref对应值
                        if ($firstElement['@attributes']['ref'] == $data['@attributes']['name']) {
                            if (isset($data['@attributes']['ref'])) {
                                $firstDom[$topName][] = detailDom($data, $include);
                            } elseif (count($data) > 1 && isset($data['complexType']))//下面还有节点情况
                            {
                                $firstDom[$topName][] = detailDom($data, $include);
                            } else {
                                $firstDom[$topName][] = $data['@attributes']['name'];
                            }
                        }
                    } else {//一维数组情况
                        $data['name'] = isset($data['name']) ? $data['name'] : '';
                        if ($firstElement['@attributes']['ref'] == $data['name']) {
                            if (isset($firstElement['@attributes']['ref'])) {
                                $firstDom[$topName][] = detailDom($data, $include);
                            } elseif (count($data) > 1 && isset($data['complexType']))//下面还有节点情况
                            {
                                $firstDom[$topName][] = detailDom($data, $include);
                            } else {
                                $firstDom[$topName][] = $data['name'];
                            }
                        }
                    }
                }
            }
        } else {
            $firstDom[$topName][] = $firstElement['@attributes']['name'] ?? '';
            if (count($firstElement) > 1 && isset($firstElement['complexType']))//下面还有节点情况
            {
                $firstDom[$topName][] = detailDom($firstElement, $include);
            } elseif (isset($firstElement['type'])) {
                $firstDom[$topName][] = $firstElement['@attributes']['name'];
                //待处理.........
            }
        }

    } else {
        foreach ($firstElement as $key => $item) {
            //二维数组多值情况
            //是否有ref引用
            if (isset($item['@attributes']['ref'])) {
                //遍历所有xsd文件查找对应ref
                $Xsd = new \app\index\controller\Xsd();
                foreach ($include as $value) {
                    $base = $Xsd->xsdArr('./xsd/' . $value);
                    $onlyOne = $base['element'];
                    foreach ($onlyOne as $k => $data) {
                        if (isset($data['@attributes']))//二维数组情况
                        {
                            //在该文件有没有和主文件ref对应值
                            if ($item['@attributes']['ref'] == $data['@attributes']['name']) {
                                if (isset($data['@attributes']['ref'])) {
                                    $firstDom[$topName][] = detailDom($data, $include);
                                } elseif (count($data) > 1 && isset($data['complexType']))//下面还有节点情况
                                {
                                    $firstDom[$topName][] = detailDom($data, $include);
                                } else {
                                    $firstDom[$topName][] = $data['@attributes']['name'];
                                }
                            }
                        } else {//一维数组情况
                            $data['name'] = isset($data['name']) ? $data['name'] : '';
                            if ($item['@attributes']['ref'] == $data['name']) {
                                if (isset($firstElement['@attributes']['ref'])) {
//                                    $firstDom[$topName][$key] = $data['ref'];
                                    $firstDom[$topName][] = detailDom($data, $include);
                                } elseif (count($data) > 1 && isset($data['complexType']))//下面还有节点情况
                                {
                                    $firstDom[$topName][] = detailDom($data, $include);
                                } else {
                                    $firstDom[$topName][] = $data['name'];
                                }
                            }
                        }
                    }
                }
            } else {
                $firstDom[$topName][] = $item['@attributes']['name'];
                if (count($item) > 1 && isset($item['complexType']))//下面还有节点情况
                {
                    $firstDom[$topName][] = detailDom($item, $include);
                } elseif (isset($item['simpleType'])) {
                    $firstDom[$topName][] = $item['@attributes']['name'];
                    //待处理........
                } elseif (isset($item['@attributes']['type'])) {
                    $firstDom[$topName][] = $item['@attributes']['name'];
                    //待处理........
                }
            }
        }
    }

    return $firstDom;
}


function detailWithDom($element, $include = null, $fileName)
{
    ini_set('memory_limit', '4096M');
    //获取所有文件结构后进行判断
    //1 首先获得第一层所有节点（顶级不可能存在ref引用）
    $topName = $element['@attributes']['name'];
    if (isset($element['complexType']['sequence']['element'])) {
        $firstElement = $element['complexType']['sequence']['element'];
    } elseif (isset($element['complexType']['choice']['element'])) {
        $firstElement = $element['complexType']['choice']['element'];
    } elseif (isset($element['sequence']['element'])) {
        $firstElement = $element['sequence']['element'];
    } else {
        $firstElement = $element['complexType']['simpleContent']['extension'];
    }
    //判断数据结构
    if (!isset($firstElement[0])) {
        if (isset($firstElement['@attributes']['ref'])) {
            //遍历所有xsd文件查找对应ref
            $Xsd = new \app\index\controller\Xsd();
            foreach ($include as $value) {
                $base = $Xsd->xsdArr('./xsd/' . $value);
                $onlyOne = $base['element'];
                if(!isset($onlyOne[0]))
                {
                    //在该文件有没有和主文件ref对应值
                    if ($firstElement['@attributes']['ref'] == $onlyOne['@attributes']['name']) {
                        if (count($onlyOne) > 1 && isset($onlyOne['complexType']))//下面还有节点情况
                        {
                            $firstDom[$topName][] = detailWithDom($onlyOne, $include, $fileName);
                        } elseif (isset($onlyOne['@attributes']['name']) && !isset($onlyOne['@attributes']['type'])) {
                            //最终节点
                            $firstDom[$topName][] = $onlyOne['@attributes']['name'];
                        } elseif (isset($onlyOne['@attributes']['type'])) {
                            //待处理
                            $firstDom[$topName][] = $onlyOne['@attributes']['name'];
                                $firstDom[$topName][] = checkType($firstDom,$include,$fileName,$onlyOne);
                        }
                    }
                }else{
                    foreach ($onlyOne as $k => $data) {
                        //在该文件有没有和主文件ref对应值
                        if ($firstElement['@attributes']['ref'] == $data['@attributes']['name']) {
                            if (count($data) > 1 && isset($data['complexType']))//下面还有节点情况
                            {
                                $firstDom[$topName][] = detailWithDom($data, $include, $fileName);
                            } elseif (isset($data['@attributes']['name']) && !isset($data['@attributes']['type'])) {
                                //最终节点
                                $firstDom[$topName][] = $data['@attributes']['name'];
                            } elseif (isset($data['@attributes']['type'])) {
                                //待处理
                                $firstDom[$topName][] = $data['@attributes']['name'];
                                $firstDom[$topName][] = checkType($firstDom,$include,$fileName,$data);
                            }
                        }
                    }
                }

            }
        } else {
            if (count($firstElement) > 1 && isset($firstElement['complexType']))//下面还有节点情况
            {
                $firstDom[$topName][] = detailWithDom($firstElement, $include, $fileName);
            } elseif (!isset($firstElement['@attributes']['type']) && isset($firstElement['@attributes']['name'])) {
                //结束并带有属性值这里可以获取
                $firstDom[$topName][] = $firstElement['@attributes']['name'];
            } elseif (isset($item['@attributes']['type'])) {
                $firstDom[$topName][] = $firstElement['@attributes']['name'];
                //待处理........
                    $firstDom[$topName][] = checkType($firstDom,$include,$fileName,$firstElement);
            }else{
                $firstDom[$topName][$firstElement['attribute']['@attributes']['name']] = $firstElement['attribute']['@attributes']['name'];
            }
        }
    }else{
        //二维数组
        foreach ($firstElement as $key => $item) {
            //是否有ref引用
            if (isset($item['@attributes']['ref'])) {
                //遍历所有xsd文件查找对应ref
                $Xsd = new \app\index\controller\Xsd();
                foreach ($include as $value) {
                    $base = $Xsd->xsdArr('./xsd/' . $value);
                    $onlyOne = $base['element'];
                    if (!isset($onlyOne[0])) {
                        //在该文件有没有和主文件ref对应值
                        if ($item['@attributes']['ref'] == $onlyOne['@attributes']['name']) {
                            if (count($onlyOne) > 1 && isset($onlyOne['complexType']))//下面还有节点情况
                            {
                                $firstDom[$topName][] = detailWithDom($onlyOne, $include, $fileName);
                            } elseif (isset($onlyOne['name']) && !isset($onlyOne['@attributes']['type'])) {
                                //最终节点
                                $firstDom[$topName][] = $onlyOne['@attributes']['name'];
                            } elseif (isset($onlyOne['@attributes']['type'])) {
                                //待处理
                                $firstDom[$topName][] = $onlyOne['@attributes']['name'];
                                    $firstDom[$topName][] = checkType($firstDom,$include,$fileName,$onlyOne);

                            }
                        }
                    } else {
                        foreach ($onlyOne as $k => $data) {

                            //在该文件有没有和主文件ref对应值
                            if ($item['@attributes']['ref'] == $data['@attributes']['name']) {
                                if (count($data) > 1 && isset($data['complexType']))//下面还有节点情况
                                {
                                    $firstDom[$topName][] = detailWithDom($data, $include, $fileName);
                                } elseif (isset($data['@attributes']['name']) && !isset($data['@attributes']['type'])) {
                                    //最终节点
                                    $firstDom[$topName][] = $data['@attributes']['name'];
                                } elseif (isset($data['@attributes']['type'])) {
                                    //待处理
                                    $firstDom[$topName][] = $data['@attributes']['name'];
                                    $firstDom[$topName][] = checkType($firstDom,$include,$fileName,$data);
                                }
                            }

                        }
                    }
                }
            } else {
                if (count($item) > 1 && isset($item['complexType']))//下面还有节点情况
                {
                    $firstDom[$topName][] = detailWithDom($item, $include, $fileName);
                } elseif (!isset($item['@attributes']['type']) && isset($item['@attributes']['name'])) {
                    //结束并带有属性值这里可以获取
                    $firstDom[$topName][] = $item['@attributes']['name'];
                } elseif (isset($item['@attributes']['type'])) {
                    //待处理
                    $firstDom[$topName][] = $item['@attributes']['name'];
                    $firstDom[$topName][] = checkType($firstDom,$include,$fileName,$item);
                }
            }


        }
    }
    return $firstDom;

}

//递归type
function checkType($firstDom, $include, $fileName, $item)
{
//    $firstDom[] = $item['@attributes']['name'];
    $typeInclude = $include;
    array_push($typeInclude, 'Product.xsd');
    $key = array_search($fileName . '.xsd', $typeInclude);
    unset($typeInclude[$key]);
    //遍历所有xsd文件查找对应ref
    $Xsd = new \app\index\controller\Xsd();
    foreach ($typeInclude as $type) {
        $base = $Xsd->xsdArr('./xsd/' . $type);
        if ($type == 'Product.xsd') {
            $oneType = $base['element'];
        } else {
            $oneType = $base['complexType'];
        }
        if (isset($oneType[0])) {
            foreach ($oneType as $findType) {
                if ($item['@attributes']['type'] == $findType['@attributes']['name']) {
                    if (count($findType) > 1 && isset($findType['sequence']))//下面还有节点情况
                    {
                        $firstDom[] = detailWithDom($findType, $include, $fileName);
                    } else {
                        $firstDom[] = $item['@attributes']['name'];
                    }
                }
            }
        } else {
            if ($item['@attributes']['type'] == $oneType['@attributes']['name']) {
                if (count($oneType) > 1 && isset($oneType['sequence']))//下面还有节点情况
                {
                    $firstDom[] = detailWithDom($oneType, $include, $fileName);
                } else {
                    $firstDom[] = $item['@attributes']['name'];
                }
            }
        }
    }
    return $firstDom;
}